#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
#include <linux/fs.h>
#include <linux/statfs.h>
#include <linux/mount.h>
#include <linux/ext2_fs.h>
#include <linux/buffer_head.h>
#include <linux/pagemap.h>
#include <asm/uaccess.h>
#include "lab4fs.h"

#define mydbg(fmt, args...)   printk(fmt , ## args)

void dump_page(struct page *page)
{
	printk("\n%s:  enter, page index  %ld\n", __func__, page->index);

	printk("\npage belongs to inode %ld\n", page->mapping->host->i_ino);

	printk("\nPage: [%s] [%s] [%s] [%s]\n",
	       PageUptodate(page) ? "Uptodate" : "Not Uptodate",
	       PageDirty(page) ? "Dirty" : "Not Dirty",
	       PageWriteback(page) ? "PageWriteback Set" :
	       "PageWriteback Cleared",
	       PageLocked(page) ? "Locked" : "Unlocked");

}

void dump_file(struct file *filp)
{

	struct inode *ind;

	if (filp->f_dentry) {
		printk("\tfilp->dentry_name = %s\n", filp->f_dentry->d_iname);

		printk("\nfilp->mapping = %p, map-host = %p\n",
		       filp->f_mapping, filp->f_mapping->host);
	}

	if (!filp->f_mapping || !filp->f_mapping->host)
		return;

	/// filp->f_mapping->host;
	ind = filp->f_dentry->d_inode;	// f_mapping->host;

	printk("\nf-map-host %ld, f-fmap-aops = %p\n", ind->i_ino,
	       filp->f_mapping->a_ops);

}

void dump_inode(struct inode *ind)
{
	int i;
	struct lab4fs_inode *li;

	if (ind == NULL) {
		printk("Error!!  ind= NULL\n");
		return;
	}
	printk("\ninod_ino = %ld, size=%d,  nlink %d, blk-num %ld, ",
	       ind->i_ino, (int)i_size_read(ind), ind->i_nlink, ind->i_blocks);

	if (ind->i_blocks > 0) {
		li = (struct lab4fs_inode *)(ind->u.generic_ip);

		for (i = 0; i < ind->i_blocks; i++)
			printk(" %d ", li->i_block[i]);
	}
	printk("\n");
}

void dump_dentry(struct dentry *dent)
{
	int ino;

	if (dent == NULL) {
		printk("%s: Error!! dent==NULL\n", __func__);
		return;
	}
	if (dent->d_inode)
		ino = dent->d_inode->i_ino;
	else
		ino = -1;

	printk("\ndent-name %s, ino %d\n", dent->d_iname, ino);

}
